<template>
  <div class="active">
    <!-- 项目信息 -->
    <h3>项目信息</h3>
    <section class="section">
      <el-form :model="projectInfo" label-width="120px">
        <el-row :gutter="20">
          <el-col :span="12">
            <el-form-item label="项目编号">
              {{ projectInfo.oaCode }}
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="项目状态">
              {{ projectInfo.status }}
            </el-form-item>
          </el-col>
        </el-row>
        <el-row :gutter="20">
          <el-col :span="24">
            <el-form-item label="项目名称">
              {{ projectInfo.name }}
            </el-form-item>
          </el-col>
        </el-row>
        <el-row :gutter="20">
          <el-col :span="12">
            <el-form-item label="发布时间">
              {{ projectInfo.publishTime }}
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="征集截止日期">
              {{ projectInfo.applyEndTime }}
            </el-form-item>
          </el-col>
        </el-row>
        <el-row :gutter="20">
          <el-col :span="12">
            <el-form-item label="征集起始日期">
              {{ projectInfo.applyStartTime }}
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="评审日期">
              {{ projectInfo.auditTime }}
            </el-form-item>
          </el-col>
        </el-row>
        <el-row :gutter="20">
          <el-col :span="12">
            <el-form-item label="查看预公告">
              <span
                class="link"
                style="margin: 0"
                @click="downloadFile(projectInfo.noticeUrl)"
              >
                {{ `《${projectInfo.name}预公告》` }}</span
              >
            </el-form-item>
          </el-col>
          <el-col :span="12" v-if="projectInfo.resultNoticeUrl">
            <el-form-item label="查看结果公告">
              <span
                class="link"
                style="margin: 0"
                @click="downloadFile(projectInfo.resultNoticeUrl)"
              >
                {{ `《${projectInfo.name}结果公告》` }}</span
              >
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
    </section>

    <!-- <h3>已下载项目书供应商资料</h3>
    <el-table :data="downloadList" stripe style="width: 100%; margin-top: 20px">
      <el-table-column prop="key" label="序号" align="center"></el-table-column>
    </el-table> -->
    <h3>已报名供应商资料</h3>
    <el-table
      :data="projectInfo.supplierList"
      stripe
      style="width: 100%; margin-top: 20px"
    >
      <el-table-column prop="key" label="序号" align="center"></el-table-column>
      <el-table-column
        prop="applyTime"
        label="报名时间"
        align="center"
      ></el-table-column>
      <el-table-column
        prop="supplierCode"
        label="供应商编码"
        align="center"
      ></el-table-column>
      <el-table-column
        prop="name"
        label="报名供应商"
        align="center"
      ></el-table-column>
      <el-table-column
        prop="supplierName"
        label="报名人姓名"
        align="center"
      ></el-table-column>
      <el-table-column
        prop="supplierPhone"
        label="手机"
        align="center"
      ></el-table-column>
      <el-table-column
        prop="supplierEmail"
        label="邮箱"
        align="center"
      ></el-table-column>
      <el-table-column label="操作" width="80" fixed="right" align="center">
        <template #default="scope">
          <el-button type="text" @click="handleView(scope.$index, scope.row)"
            >查看</el-button
          >
        </template>
      </el-table-column>
    </el-table>

    <h3>项目评审人员</h3>
    <el-table
      :data="projectInfo.judgeList"
      stripe
      style="width: 100%; margin-top: 20px"
    >
      <el-table-column prop="key" label="序号" align="center"></el-table-column>
      <el-table-column
        prop="startCheckTime"
        label="签到时间"
        align="center"
      ></el-table-column>
      <el-table-column label="查看亲签" width="100" align="center">
        <template #default="scope">
          <el-button type="text" @click="signature('startCheck', scope.row)"
            >查看</el-button
          >
        </template>
      </el-table-column>
      <el-table-column
        prop="type"
        label="评审角色"
        align="center"
      ></el-table-column>
      <el-table-column
        prop="name"
        label="姓名"
        align="center"
      ></el-table-column>
      <el-table-column
        prop="phone"
        label="手机"
        align="center"
      ></el-table-column>
      <el-table-column
        prop="endCheckTime"
        label="签名确认时间"
        align="center"
      ></el-table-column>
      <el-table-column
        label="查看亲签"
        width="100"
        fixed="right"
        align="center"
      >
        <template #default="scope">
          <el-button type="text" @click="signature('endCheck', scope.row)"
            >查看</el-button
          >
        </template>
      </el-table-column>
    </el-table>

    <h3>项目评审结果</h3>
    <el-table
      :data="projectInfo.supplierList"
      stripe
      style="width: 100%; margin-top: 20px"
    >
      <el-table-column prop="key" label="序号" align="center"></el-table-column>
      <el-table-column
        prop="applyTime"
        label="报名时间"
        align="center"
      ></el-table-column>
      <el-table-column
        prop="supplierCode"
        label="供应商编码"
        align="center"
      ></el-table-column>
      <el-table-column
        prop="name"
        label="报名供应商"
        align="center"
      ></el-table-column>
      <el-table-column
        prop="status"
        label="资质审查"
        align="center"
      ></el-table-column>
      <el-table-column label="报价" align="center">
        <template #default="scope">
          {{ scope.row.price || "/" }}
        </template>
      </el-table-column>
      <el-table-column
        prop="downloadTime"
        label="唱价时间"
        align="center"
      ></el-table-column>
      <el-table-column
        prop="result"
        label="评审结果"
        align="center"
      ></el-table-column>
    </el-table>

    <!-- <h3>项目评审材料</h3>
    <el-table
      :data="material"
      stripe
      style="width: 100%; margin-top: 20px"
    >
      <el-table-column prop="key" label="序号" align="center"></el-table-column>
      <el-table-column
        prop="time"
        label="评审时间"
        align="center"
      ></el-table-column>
      <el-table-column
        prop="name"
        label="项目评审文件名称"
        align="center"
      ></el-table-column>
      <el-table-column label="操作" width="80" fixed="right" align="center">
        <template #default="scope">
          <el-button type="text" @click="downloadFile(scope.row)"
            >查看</el-button
          >
        </template>
      </el-table-column>
    </el-table> -->

    <h3 v-if="projectInfo.ratifyExecTime||projectInfo.breakRemark">项目中止结果原因</h3>
    <el-form
      :model="form"
      label-width="195px"
      v-if="projectInfo.ratifyExecTime||projectInfo.breakRemark"
    >
      <el-form-item label="中选供应商名称：">
        {{ projectInfo.judgename }}
        <!-- <el-input v-model="projectInfo.judgename" disabled></el-input> -->
      </el-form-item>
      <el-form-item label="项目中止理由：">
        {{ projectInfo.breakRemark }}
        <!-- <el-input
          v-model="projectInfo.breakRemark"
          type="textarea"
          disabled
        ></el-input> -->
      </el-form-item>
    </el-form>
    <div class="endbutton">
      <el-button type="primary" @click="navigator(`/judgeProject?type=${type}`)"
        >返回</el-button
      >
    </div>
  </div>
</template>
<script setup>
import { ref } from "vue";
import { useRoute, useRouter } from "vue-router";
import {
  totalScore,
  projectInfoAndSupplierAndJudge,
  queryTaskId,
} from "@/api/examine.js";
const baseUrl = import.meta.env.VITE_APP_BASE_API;
import { judgeProjectInfo } from "@/api/project.js";
import { getDictList, getDict, nowTime, getdict } from "@/utils/common.js";
import { ElMessage } from "element-plus";
const type = computed(() => route.query.type);

const router = useRouter();
const emit = defineEmits(["next-step"]);
const route = useRoute();
const id = computed(() => route.query.id);
// 定义数据
const projectInfo = ref({});
const downloadList = ref([]);
const material = ref([]);
const baseURL = import.meta.env.VITE_APP_BASE_API;
const getInfo = (boolean) => {
  judgeProjectInfo({ id: id.value }).then((res) => {
    if (res.code == "200") {
      projectInfo.value = res.data;
      for (let i = 0; i < res.data.supplierList.length; i++) {
        projectInfo.value.supplierList[i].key = i + 1;
        projectInfo.value.supplierList[i].status = getDict(
          "apply_supplier_status",
          res.data.supplierList[i].status
        );
        if (
          projectInfo.value.outbidOneId ==
          projectInfo.value.supplierList[i].supplierId
        ) {
          projectInfo.value.judgename = projectInfo.value.supplierList[i].name;
          projectInfo.value.supplierList[i].result = "中选";
        } else {
          projectInfo.value.supplierList[i].result = "未中选";
        }
      }
      for (let i = 0; i < res.data.judgeList.length; i++) {
        res.data.judgeList[i].key = i + 1;
        projectInfo.value.judgeList[i].type = getDict(
          "judge_user_type",
          res.data.judgeList[i].type
        );
      }
      for (let i = 0; i < res.data.fileList.length; i++) {
        console.log(res.data.fileList[i].fileUrl);

        material.value.push({
          key: i + 1,
          time: res.data.fileList[i].createTime,
          name: res.data.fileList[i].fileUrl.split("/").pop(),
          url: res.data.fileList[i].fileUrl,
        });
      }
      if (!projectInfo.value.judgename) projectInfo.value.judgename = "无";
      projectInfo.value.status = getDict(
        "project_status",
        projectInfo.value.status
      );
    }
  });
};
const handleView = (item, data) => {
  const url = `/judgedetail?id=${id.value}&type=judgeInfo&judgeid=${data.id}`;
  window.open(url, "_blank"); // 在新窗口中打开
};
const signature = (item, data) => {
  console.log(item);
  console.log(data);
  let url = baseUrl;
  if (item == "startCheck") {
    url += data.startCheck;
  } else {
    url += data.endCheck;
  }
  window.open(url, "_blank"); // 在新窗口中打开
};

const downloadFile = async (file) => {
  let Url = "";
  Url = file;
  // Fetch the file data as a blob
  const response = await fetch(`${baseUrl}${Url}`);
  if (!response.ok) throw new Error("下载失败");

  const blob = await response.blob();
  const url = URL.createObjectURL(blob);

  // Create a temporary link for downloading
  const link = document.createElement("a");
  link.href = url;
  link.download = Url.split("/").pop(); // Extract file name
  link.click();

  // Revoke the URL after download
  URL.revokeObjectURL(url);
};
const navigator = (path) => {
  router.push(path);
};
const Previous = ref("");
onMounted(() => {
  getInfo();
  queryTaskId({ projectId: id.value }).then((res) => {
    Previous.value = res.data;
  });
});
</script>

<style scoped lang="scss">
@use "./judge";
</style>
